package com.sxt;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.copy.Jsoup1;
import com.sxt.copy.Jsoup2;
import com.sxt.entity.Article;
import com.sxt.service.ArticleService;

public class qukuaiwang {
	 // 获取img标签正则
    private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
	private final static String BiUrl = "http://www.qukuaiwang.com.cn/News/index/p/16.html";
	private static String biUrl = "http://www.qukuaiwang.com.cn";
	public static List<String> getUrl() throws IOException{
		Document doc = Jsoup.connect(BiUrl).get();
		Elements els = doc.select("div.list-art");
		List<String> list = new ArrayList<String>();
		for (Element element : els) {
			String pageUrl = element.select("a.img-article").attr("href");
			
			  list.add(biUrl+pageUrl);
			
		}
		return list;
	}
	 public static List<String> getImageUrl(String html) {
    	 Matcher matcher=Pattern.compile(IMGURL_REG).matcher(html);
         List<String>listimgurl=new ArrayList<String>();
         while (matcher.find()){
             listimgurl.add(matcher.group());
         }
         return listimgurl;
	}
   //获取ImageSrc地址
     public static List<String> getImageSrc(List<String> listimageurl){
         List<String> listImageSrc=new ArrayList<String>();
         for (String url : listimageurl) {
        	 Document doc = Jsoup.parse(url);
        	 Elements link = doc.select("img");
        	 String src = link.attr("src");
        	 listImageSrc.add("http://www.qukuaiwang.com.cn"+src);
		}
        
         return listImageSrc;
     }
    public static void add(String url){
    	 List<String> list = Jsoup1.addArticle(url, 
					"div.contents h1", 
					"div.content-art");
    	 String html = list.get(1);
    	 List<String> srclist = getImageSrc(getImageUrl(html));
    	 Document doc = Jsoup.parse(html);
    	 doc.select("div.artOthers").html("");
    	 Elements link = doc.select("img");
    	 int s=0;
    	  for (Element ele : link) {
    		  ele.attr("src", srclist.get(s++));
		}
    	  String content = doc.toString();
    	 
 	//启动Spring容器
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		ArticleService articleService=(ArticleService) context.getBean("articleService");
		Article art =new Article();
		
		String id =String.valueOf((int) (Math.random()*1000000000));
       art.setId(id);
       art.setTitle(list.get(0));
       art.setTags("区块链");
       art.setAuthorId("1533707196255");
       art.setContent(content);
       art.setCommentCount(0);
   	  art.setViewCount(0);
       art.setRewardContent("");
   	  art.setRewardPoint(0);
       art.setPermalink("/article/"+art.getId());
       Long l = System.currentTimeMillis();
        String date = l.toString();
   	  art.setCreateTime(new BigInteger(date));
       art.setUpdateTime(new BigInteger(date));
   	  art.setLatestCmtTime(new BigInteger("0"));
       art.setLatestCmterName("");
       art.setRandomDouble(Math.random());
       art.setCommentable("1");
       
       art.setEditorType(0);
       art.setArticleStatus(0);
   	  art.setArticleType(0);
       art.setGoodCnt(0);
   	  art.setBadCnt(0);
       art.setCollectCnt(0);
       art.setWatchCnt(0);
       art.setRedditScore(0d);
       art.setCity("北京");
       art.setIP("123.7.5.187");
       art.setUA("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
       art.setStick(new BigInteger("0"));
       art.setAnonymous(0);
       art.setPerfect(0);
       art.setAnonymousView(0);
       art.setAudioURL("");
       art.setQnAOfferPoint(0);
       art.setPushOrder(0);
       art.setImg1URL("");
		articleService.register(art);
    	
    	
    }
    
	public static void main(String[] args) throws Exception{
		 for (String url : getUrl()) {
   	          add(url);
	        }
		/*List<String> urls = new ArrayList<String>();
		 
		 urls.add("http://www.qukuaiwang.com.cn/news/11002.html");
		
		
		 */
		 
		

		 
		
		 
		 
		/* for (String url : urls) {
			 add(url);
		}*/
     
     }
}
